package aos.ydksxt.service.xxdjgl;

import aos.framework.core.typewrap.Dto;
import aos.framework.core.typewrap.Dtos;
import aos.framework.core.utils.AOSJson;
import aos.framework.core.utils.AOSUtils;
import aos.framework.web.router.HttpModel;
import aos.ydksxt.dao.DjFcsyqdjxx;
import aos.ydksxt.service.common.CommonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 房屋所有权登记管理服务
 * Created by Stiofan on 2016/12/25.
 */
@Service
public class FcsyqdjglService extends CommonService {
    @Override
    @Transactional
    public void initView(HttpModel httpModel) {
        httpModel.setViewPath("/ydksxt/xxdjgl/fcsyqdjgl.jsp");
    }
    @Transactional
    public void queryFcsyqdjxxByFcsyquuid(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto();
        param.put("fcsyquuid", inDto.get("fcsyquuid"));
        DjFcsyqdjxx djFcsyqdjxx = (DjFcsyqdjxx) persistenceDAO.findSingleByHql("from DjFcsyqdjxx xx where xx.fcsyquuid=:fcsyquuid and xx.yxbz='Y'", param);
        Dto outDto = Dtos.newOutDto();
        outDto.setAppCode("suc");
        outDto.putAll((Map) AOSUtils.object2Map(djFcsyqdjxx));
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
    @Transactional
    public void queryFcsyqdjxxByDjxh(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("djxh", inDto.get("djxh"));
        List results = persistenceDAO.findAllBySql("select xx.*,\n" +
                "       syq.fcsyquuid,\n" +
                "       syq.fcsyq_qdrq,\n" +
                "       syq.fcsyq_msmj,\n" +
                "       syq.fcsyq_msje,\n" +
                "       syq.fcsyq_qsrq,\n" +
                "       syq.fcsyq_zzrq,\n" +
                "       xx.fczyz - syq.fcsyq_msje fcsyq_ysyz\n" +
                "  from ydks.dj_fcsyqdjxx syq, ydks.dj_fcjcxx xx\n" +
                " where xx.fcbh = syq.fcbh\n" +
                "   and xx.yxbz = 'Y'\n" +
                "   and syq.yxbz = 'Y'\n" +
                "   and syq.fcsyrlx_dm not in ('05', '06')\n" +
                "   and syq.djxh = :djxh", param);
        for (Object object : results) {
            Map<String, Object> map = (Map<String, Object>) object;
            String xzqhdsDm = (String) map.get("xzqhdsDm");
            String xzqhxqDm = (String) map.get("xzqhxqDm");
            String jdxzDm = (String) map.get("jdxzDm");
            String dzSq = (String) map.get("zldzSqjqDm");
            String dlxd = (String) map.get("zldzDlxdDm");
            String dzLh = (String) map.get("zldzLh");
            map.put("zldz", getFullAddress(xzqhdsDm, xzqhxqDm, jdxzDm, dzSq, dlxd, dzLh));
        }
        httpModel.setOutMsg(AOSJson.toJson(AOSUtils.object2Map(results)));
    }
    @Transactional
    public void saveFcsyqdjxx(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto params = Dtos.newDto("fcsyquuid", inDto.get("fcsyquuid"));
        DjFcsyqdjxx djFcsyqdjxx = (DjFcsyqdjxx) persistenceDAO.findSingleByHql("from DjFcsyqdjxx xx where xx.fcsyquuid=:fcsyquuid", params);
        if (djFcsyqdjxx == null) {
            djFcsyqdjxx = new DjFcsyqdjxx();
        }
        AOSUtils.copyProperties(inDto, djFcsyqdjxx);
        djFcsyqdjxx.setYxbz("Y");
        if (StringUtils.isEmpty(djFcsyqdjxx.getFcsyquuid())) {
            djFcsyqdjxx.setFcsyquuid(getRandomUUID());
            djFcsyqdjxx.setLrrDm(getCzryDm(httpModel));
            djFcsyqdjxx.setLrrq(getDateTime());
            djFcsyqdjxx.setSjgsdq(getCzjgDm(httpModel));
        } else {
            djFcsyqdjxx.setXgrDm(getCzryDm(httpModel));
            djFcsyqdjxx.setXgrq(getDateTime());
        }
        Dto outDto = Dtos.newOutDto();
        Dto checkParams = Dtos.newDto();
        checkParams.put("fcbh", inDto.get("fcbh"));
        List<DjFcsyqdjxx> fcsyqs = persistenceDAO.findAllByHql("from DjFcsyqdjxx xx where xx.fcbh=:fcbh and xx.yxbz='Y'", checkParams);
        BigDecimal fcysymj = djFcsyqdjxx.getFcsyqYsmj().add(djFcsyqdjxx.getFcsyqMsmj());
        for (DjFcsyqdjxx fcsyq : fcsyqs) {
            if (!fcsyq.getFcsyquuid().equals(djFcsyqdjxx.getFcsyquuid())) {//累计已经使用的土地面积，如果累计面积大于土地面积，则不允许保存
                fcysymj.add(fcsyq.getFcsyqYsmj().add(fcsyq.getFcsyqMsmj()));
                if (StringUtils.compare(String.valueOf(djFcsyqdjxx.getDjxh()), String.valueOf(fcsyq.getDjxh())) == 0 &&
                        StringUtils.compare(djFcsyqdjxx.getFcsyqlxDm(), fcsyq.getFcsyqlxDm()) == 0 &&
                        StringUtils.compare(djFcsyqdjxx.getFcsyrlxDm(), fcsyq.getFcsyrlxDm()) == 0) {
                    outDto.setAppCode("err");
                    outDto.setAppMsg("同一纳税人、使用人类型、使用权类型不允许存在多条重复数据，请直接在原基础上进行修改!");
                    break;
                }
                //校验同一人是否存在土地使用权交叉情况，如果存在则不允许保存
                if (StringUtils.compare(djFcsyqdjxx.getFcsyrlxDm(), fcsyq.getFcsyrlxDm()) == 0 &&
                        StringUtils.compare(djFcsyqdjxx.getFcsyqlxDm(), fcsyq.getFcsyqlxDm()) == 0) {
                    if (djFcsyqdjxx.getFcsyqQsrq().before(fcsyq.getFcsyqQsrq()) && djFcsyqdjxx.getFcsyqZzrq().after(fcsyq.getFcsyqQsrq()) &&
                            (djFcsyqdjxx.getFcsyqQsrq().before(fcsyq.getFcsyqZzrq()) && djFcsyqdjxx.getFcsyqZzrq().after(fcsyq.getFcsyqZzrq()) &&
                                    (djFcsyqdjxx.getFcsyqQsrq().after(fcsyq.getFcsyqQsrq()) && djFcsyqdjxx.getFcsyqZzrq().before(fcsyq.getFcsyqZzrq()) &&
                                            (djFcsyqdjxx.getFcsyqQsrq().before(fcsyq.getFcsyqQsrq()) && djFcsyqdjxx.getFcsyqZzrq().after(fcsyq.getFcsyqZzrq()))))) {
                        outDto.setAppCode("err");
                        outDto.setAppMsg("针对同一使用人类型及使用权类型，不允许存在多条登记信息!");
                        break;
                    }
                }
            }
        }
        if (fcysymj.compareTo(inDto.getBigDecimal("jzmj")) > 0) {
            outDto.setAppCode("err");
            outDto.setAppMsg("本次登记的使用权面积与已登记的使用权面积之和(" + fcysymj + ")大于房产建筑面积(" + inDto.getBigDecimal("jzmj") + "),请检查所填应税面积、免税面积是否正确!");
        }
        if (!"err".equals(outDto.getAppCode())) {
            try {
                persistenceDAO.saveOrUpdate(djFcsyqdjxx);
                outDto.setAppCode("suc");
            } catch (Exception e) {
                outDto.setAppCode("err");
                outDto.setAppMsg("房产使用权信息保存失败!");
                logger.error(e.getMessage());
            }
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
    @Transactional
    public void zfFcsyqdjxxByFcsyquuid(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto params = Dtos.newDto("fcsyquuid", inDto.get("fcsyquuid"));
        DjFcsyqdjxx djFcsyqdjxx = (DjFcsyqdjxx) persistenceDAO.findSingleByHql("from DjFcsyqdjxx xx where xx.fcsyquuid=:fcsyquuid", params);
        djFcsyqdjxx.setYxbz("N");
        djFcsyqdjxx.setXgrDm(getCzryDm(httpModel));
        djFcsyqdjxx.setXgrq(getDateTime());
        djFcsyqdjxx.setZfrDm(getCzryDm(httpModel));
        djFcsyqdjxx.setZfrq(getDateTime());
        djFcsyqdjxx.setZfyy(inDto.getString("zfyy"));
        Dto outDto = Dtos.newOutDto();
        try {
            persistenceDAO.saveOrUpdate(djFcsyqdjxx);
            outDto.setAppCode("suc");
        } catch (Exception e) {
            outDto.setAppCode("err");
            outDto.setAppMsg("房产使用权信息作废失败!");
            logger.error(e.getMessage());
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
}
